package com.restaurant.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.restaurant.domain.Menu;
import com.restaurant.proxies.MenuItemProxy;

public class MenuItemFormValidator implements Validator{

	
	@Override
	public boolean supports(Class<?> arg0) {
		return Menu.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors arg1) {
		ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "menu.menuname", "menu.menuname");
		ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "menu.price", "menu.price");		
		
		MenuItemProxy menuItemProxy = (MenuItemProxy) arg0;
		if(menuItemProxy.getMenu().getMenucategory().getMenucategoryID() == 0)
			ValidationUtils.rejectIfEmpty(arg1, "menu.menucategory.menucategoryname", "menucategory.name.select");
		if(menuItemProxy.getMenu().getFoodtype().getFoodTypeID() == 0)
			ValidationUtils.rejectIfEmpty(arg1, "menu.foodtype.foodname", "foodtype.name.select");
		
		
	}

}
